Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types: remove void from Maybe<T> and update SuiteSelectors #1074

Merged
merged 1 commit into from
Dec 16, 2023

Conversation

vonagam
Copy link
Contributor

@vonagam vonagam commented Sep 16, 2023

Q A
Types added? ✔/✖

In #1070 I've referenced Maybe in exported type. Did not know that Maybe is an internal helper type. Because of this I've met errors of such sort:

The inferred type of 'getError' cannot be named without a reference to '.pnpm/[email protected]/node_modules/vest-utils'. This is likely not portable. A type annotation is necessary.

So this PR removes Maybe usage from SuiteSelectors.

But while doing it I've encountered resistance from typescript because Maybe<T> is currently defined as T | undefined | void. I think it is mistake to include void because it is a special thing - something like unknown but one that you should not touch at all, only discard. So I've changed Maybe to simple T | undefined.

@vercel
Copy link

vercel bot commented Sep 16, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
vest ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 15, 2023 9:45pm
vest-next ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 15, 2023 9:45pm

@ealush
Copy link
Owner

ealush commented Sep 26, 2023

Hey, @vonagam, I think I missed this diff. Sorry. Thank you for making this PR

Here's the issue I was trying to resolve using void in Maybe:
image

Basically, TS treats undefined and void differently in function returns. Didn't dig into it, but removing void from Maybe makes it so that tests without a return value will fail the compiler.

An alternative would be to add void directly to TestResult, but that's pretty much the same thing, isn't it? Maybe there's a smarter way that I am not aware of.

@ealush
Copy link
Owner

ealush commented Sep 26, 2023

Also, I am not fully sure what would cause the issue you're seeing. Can you put a repro on codesandbox and I'll try to look at it? Wondering if it is a pnpm thing, vest thing, or something completely different.

Tried a quick repro on CodeSandbox and I didn't experiecne it, so interesting to see why it is behaving like that.

@vonagam
Copy link
Contributor Author

vonagam commented Sep 26, 2023

About repo: I have not used codesandbox much but this is part of an example that produces the error:

export class Wrapper {
  //...
  getError() { // <- mentioned error will be there
    return this.suiteResult.getError();
  }
}

Need to have "declaration": true in tsconfig as this one about reexporting types.

About no return: No return functions can be assigned to type of functions with undefined return without a problem - playground. But void functions (so typed as void) cannot be assigned to anything besides void functions - playground. So what you shown can be made work without an error but something prevents it. Edit: Intersting, () => {} can be assigned to () => undefined but not to () => undefined | boolean, feels like a bug in typescript.

@vonagam
Copy link
Contributor Author

vonagam commented Sep 26, 2023

Annoying, so in microsoft/TypeScript#36288 ts got ability to accept () => {} as () => undefined but not as () => undefined | T which I find inconsistent... There is also microsoft/TypeScript#36239 which will solve the issue.

As for now, removed modification to Maybe and just purged its usage from selectors types.

@vonagam
Copy link
Contributor Author

vonagam commented Sep 27, 2023

For record, seems like TestResult is the only time when void is needed in Maybe, so it is still possible to remove void from Maybe and just define TestResult as Maybe<...> | void.

@vonagam
Copy link
Contributor Author

vonagam commented Dec 15, 2023

Ok, two things.

First, published my integration of vest for svelte. So now it should be possible to see the errors I am talking about. Let's take this code:

getError(): Suite.Failure<V, A> | undefined {
  return this.summaryStore.value.getError() as any;
}

Ideally I would want not to specify types and let everything be inferred:

getError() {
  return this.summaryStore.value.getError();
}

But if I do that and run pnpm tsc I currently get:

The inferred type of 'getError' cannot be named without a reference to '.pnpm/[email protected]/node_modules/vest-utils'. This is likely not portable. A type annotation is necessary.

So I am required to specify a type, but if I do it like this:

getError(): Suite.Failure<V, A> | undefined {
  return this.summaryStore.value.getError();
}

Then I get an error that says:

Type 'void' is not assignable to type 'SummaryFailure<Field, string> | undefined'.

That's the problem I am trying to solve.

Second, so I've reverted PR to almost initial state where I removed void from Maybe (as title says), but, as you pointed out, changed TestResult type to be Maybe<> | void to match that use case (that's the only place where Maybe is used as a public return type for that purpose).

Thoughts?

@ealush
Copy link
Owner

ealush commented Dec 15, 2023

This seems reasonable. I created Maybe as a very simple helper utility type to help me cope with having to type things repeatedly, but OTOH, maybe that's not the best option when exposing a publicly facing API, and the best approach is to be explicit to begin with.

Let me just test this locally real quick, and if this works, I'll merge, and release a @next version just so we can test it. If everything works then, we can release a patch version early next week.

Copy link
Owner

@ealush ealush left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally, all the tests pass and the build runs correctly too. Merging, and releasing a @next version. Release will finish in 10 minutes approximately

@ealush
Copy link
Owner

ealush commented Dec 15, 2023

Also, @vonagam, the vest-for-svelte repo looks great! Thank you so much for working on it
May I add it to the community integrations section of the website?

https://vestjs.dev/docs/community_resources/integrations

@vonagam
Copy link
Contributor Author

vonagam commented Dec 15, 2023

Thanks. Yes, of course, have nothing against the library being listed there.

@ealush
Copy link
Owner

ealush commented Dec 15, 2023

OK, @vonagam,
vest@next can already be install. The exact tag is: [email protected].

I'll add the resource to the website tomorrow, I'm after a very long international flight, so it will take me just a little bit more time. Thank you for all the effort you're putting into Vest!

@vonagam
Copy link
Contributor Author

vonagam commented Dec 15, 2023

Ok, with that version I can remove as any from getError/getWarning usages. Which is good.

But seems like I still need to specify return type because Maybe is used in SummaryFailure definition. If it is replaced there with simple | undefined then errors go away.

Can I add this change? (Checking because you made a release, don't want to mess things up.)

@ealush
Copy link
Owner

ealush commented Dec 15, 2023

@vonagam, yes, sure. Go ahead. The release goes through a separate branch, no issues there.

@ealush
Copy link
Owner

ealush commented Dec 15, 2023

@vonagam - OK, @next now is [email protected]. Let's try this one.
I'll head out for today, but if that works for you, I'll get that merged and published soon.

@vonagam
Copy link
Contributor Author

vonagam commented Dec 15, 2023

Yep, works without a problem.

@ealush ealush merged commit 7f336f0 into ealush:latest Dec 16, 2023
6 of 7 checks passed
@ealush
Copy link
Owner

ealush commented Dec 16, 2023

@vonagam, released as [email protected]

@vonagam vonagam deleted the fix-maybe-type branch December 16, 2023 21:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants